#include "Event.h"
#include "VoronoiDiagram.h"


void SiteEvent::handle(VoronoiDiagram *caller) const
{
	caller->insertFace(site_);
}

void CircleEvent::handle(VoronoiDiagram *caller) const
{
	if(!edge_->face && prev_id==edge_->prev->twin->face->site_.id_ &&
		next_id==edge_->next->twin->face->site_.id_)
		caller->insertVertex(edge_, time_, center_);
}

bool EventPriorityJudge::operator()(
	Event* const & lhs, Event* const & rhs) const
{
	if (lhs->getX() < rhs->getX())
		return false;
	if (lhs->getX() > rhs->getX())
		return true;
	if (lhs->getY()>rhs->getY() || lhs->getType()==CIRCLE)
		return false;
	return true;
}

EventQueue::~EventQueue()
{
	clear();
}

void EventQueue::init(const std::vector<Site>& sites)
{
	std::vector<Site>::const_iterator iter = sites.begin();
	std::vector<Site>::const_iterator end = sites.end();
	for ( ; iter!=end; ++iter)
	{
		SiteEvent* se = new SiteEvent(*iter);
		queue_.push(se);
	}
}

void EventQueue::pop()
{
	assert (!queue_.empty());

	Event* e = queue_.top();
	queue_.pop();
	delete e;

	while (!queue_.empty())
	{
		e = queue_.top();
		if (e->isDisabled())
		{
			queue_.pop();
			delete e;
		}
		else
		{
			return;
		}
	}
}

void EventQueue::clear()
{
	while (!queue_.empty())
	{
		Event *e = queue_.top();
		queue_.pop();
		delete e;
	}
}

